package com.ambitor.grass.util.string;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;

public class StringUtils {

	public static String trimSuffix(String str, String suffix) {
		return ((str.endsWith(suffix)) ? str.substring(0, str.length() - 1)
				: str);
	}

	public static String trimPrefix(String str, String prefix) {
		return ((str.startsWith(prefix)) ? str.substring(prefix.length()) : str);
	}

	@SuppressWarnings("unchecked")
	public static IData DataToUpperCase(IData data) {
		if (data == null)
			return data;
		IData new_data = new DataMap();
		String[] names = data.getNames();
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			Object value = data.get(name);
			new_data.put(name.toUpperCase(), value);
		}
		return new_data;
	}

	@SuppressWarnings("unchecked")
	public static IData DataToLowerCase(IData data) {
		if (data == null)
			return data;
		IData new_data = new DataMap();
		String[] names = data.getNames();
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			Object value = data.get(name);
			new_data.put(name.toLowerCase(), value);
		}
		return new_data;
	}

	@SuppressWarnings("unchecked")
	public static IDataset DatasetToUpperCase(IDataset dataset)
			throws Exception {
		if (dataset == null)
			return dataset;
		IDataset new_dataset = new DatasetList();
		for (int i = 0; i < dataset.count(); i++) {
			IData old_data = dataset.getData(i);
			if(old_data ==null)
				continue;
			IData new_data = new DataMap();
			String[] names = old_data.getNames();
			for (int j = 0; j < names.length; j++) {
				String name = names[j];
				Object value = old_data.get(name);
				new_data.put(name.toUpperCase(), value);
			}
			new_dataset.add(new_data);
		}
		return new_dataset;
	}

	@SuppressWarnings("unchecked")
	public static IDataset DatasetToLowerCase(IDataset dataset)
			throws Exception {
		if (dataset == null)
			return dataset;
		IDataset new_dataset = new DatasetList();
		for (int i = 0; i < dataset.count(); i++) {
			IData old_data = dataset.getData(i);
			if(old_data ==null)
				continue;
			String[] names = old_data.getNames();
			IData new_data = new DataMap();
			for (int j = 0; j < names.length; j++) {
				String name = names[j];
				Object value = old_data.get(name);
				new_data.put(name.toLowerCase(), value);
			}
			new_dataset.add(new_data);
		}
		return new_dataset;
	}

	public static String[] StringToUpperCase(String[] keys) {
		if (keys == null || keys.length == 0)
			return keys;

		String[] new_keys = new String[keys.length];
		for (int i = 0; i < keys.length; i++) {
			new_keys[i] = (keys[i].toUpperCase());
		}
		return new_keys;
	}
	
	public static String buildFilterStr(String searchKey, String[] keys,
			boolean precise) {
		if (searchKey == null || keys == null || searchKey.trim().equals("")
				|| keys.length == 0)
			return "";
		String[] searchKeys = searchKey.split("[\\s]+");
		String symbol = "~";
		if (precise)
			symbol = "=";

		StringBuilder filterStr = new StringBuilder();
		for (String str : searchKeys) {
			for (String key : keys)
				filterStr.append(key + symbol + str + ",");
		}
		filterStr.deleteCharAt(filterStr.lastIndexOf(","));
		return filterStr.toString();
	}
}
